package com.example.anni.zhuzhoujia.xiaowan;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;
import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
import com.wx.wheelview.widget.WheelViewDialog;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Anni on 2016/7/1.
 */
public class MyInformationActivity extends AppCompatActivity {
    private EditText etReviseNickName;
    private TextView tvReviseSex;
    private TextView tvReviseEmail;
    private TextView tvReviseArea;
    private LinearLayout llChangeBgArea;
    private LinearLayout llChangeBgSex;
    private TextView tvClickFinish;
    private ImageView ivReturnArr;
    private Effectstype effect;
    private Boolean isRevise = false;

    String sex[] = {"男", "女", "保密"};

    String area[] = {
            "石峰区", "炎陵县", "云龙示范区", "株洲县", "攸县",
            "天元区", "醴陵市", "芦淞区", "荷塘区", "茶陵县"
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.revise_user_information);
        etReviseNickName = (EditText) findViewById(R.id.et_revise_nickName);
        tvReviseSex = (TextView) findViewById(R.id.tv_revise_sex);
        tvReviseEmail = (TextView) findViewById(R.id.tv_revise_email);
        tvReviseArea = (TextView) findViewById(R.id.tv_revise_area);
        llChangeBgArea = (LinearLayout) findViewById(R.id.ll_change_bg_area);
        llChangeBgSex = (LinearLayout) findViewById(R.id.ll_change_bg_sex);
        tvClickFinish = (TextView) findViewById(R.id.tv_click_finish);
        ivReturnArr = (ImageView) findViewById(R.id.iv_return_arr);

        initData();
        reviseSex();
        reviseArea();
        tvClickFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams params = new RequestParams("http://tr.zzapi.gson.cn/api/user/change/information");
                String nickName = etReviseNickName.getText().toString();
                String gender = null;
                if (tvReviseSex.getText().toString().equals("男")) {
                    gender = "BOY";
                } else if (tvReviseSex.getText().toString().equals("女")) {
                    gender = "GIRL";
                } else if (tvReviseSex.getText().toString().equals("保密")) {
                    gender = "UNKONW";
                }

                String area = null;
                if (tvReviseArea.getText().toString().equals("醴陵")) {
                    area = "LL";
                } else if (tvReviseArea.getText().toString().equals("石峰区")) {
                    area = "SF";
                } else if (tvReviseArea.getText().toString().equals("炎陵县")) {
                    area = "YL";
                } else if (tvReviseArea.getText().toString().equals("云龙示范区")) {
                    area = "YLSFFQ";
                } else if (tvReviseArea.getText().toString().equals("株洲县")) {
                    area = "ZZX";
                } else if (tvReviseArea.getText().toString().equals("攸县")) {
                    area = "YX";
                } else if (tvReviseArea.getText().toString().equals("天元区")) {
                    area = "TY";
                } else if (tvReviseArea.getText().toString().equals("芦淞区")) {
                    area = "LS";
                } else if (tvReviseArea.getText().toString().equals("荷塘区")) {
                    area = "HT";
                } else if (tvReviseArea.getText().toString().equals("茶陵县")) {
                    area = "CL";
                }

                params.addBodyParameter("nickName", nickName);
                params.addBodyParameter("gender", gender);
                params.addBodyParameter("area", area);
                x.http().post(params, new CallBackAdapter<String>() {
                    @Override
                    public void onSuccess(String s) {
                        JSONObject obj = JSON.parseObject(s);
                        Log.i("obj", obj.toString());
                        Boolean success = obj.getBoolean("success");
                        if (success) {
                            JSONObject data = obj.getJSONObject("data");
                            //设置默认的昵称
                            etReviseNickName.setText(data.getString("nickName"));
                            //设置性别
                            if (data.getString("gender").equals("BOY")) {
                                tvReviseSex.setText("男");
                            } else if (data.getString("gender").equals("GIRL")) {
                                tvReviseSex.setText("女");
                            } else if (data.getString("gender").equals("UNKONW")) {
                                tvReviseSex.setText("保密");
                            }
                            //设置邮箱
                            tvReviseEmail.setText(data.getString("email"));
                            //设置区域
                            if (data.getString("area").equals("LL")) {
                                tvReviseArea.setText("醴陵");
                            } else if (data.getString("area").equals("SF")) {
                                tvReviseArea.setText("石峰区");
                            } else if (data.getString("area").equals("YL")) {
                                tvReviseArea.setText("炎陵县");
                            } else if (data.getString("area").equals("YLSFFQ")) {
                                tvReviseArea.setText("云龙示范区");
                            } else if (data.getString("area").equals("ZZX")) {
                                tvReviseArea.setText("株洲县");
                            } else if (data.getString("area").equals("YX")) {
                                tvReviseArea.setText("攸县");
                            } else if (data.getString("area").equals("TY")) {
                                tvReviseArea.setText("天元区");
                            } else if (data.getString("area").equals("LS")) {
                                tvReviseArea.setText("芦淞区");
                            } else if (data.getString("area").equals("HT")) {
                                tvReviseArea.setText("荷塘区");
                            } else if (data.getString("area").equals("CL")) {
                                tvReviseArea.setText("茶陵县");
                            }
                            Intent intent = new Intent(MyInformationActivity.this, UserActivity.class);
                            startActivity(intent);
                            Toast.makeText(MyInformationActivity.this, "修改成功",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MyInformationActivity.this, "数据请求失败，请检查网格",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
        ivReturnArr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRevise) {

                    effect = Effectstype.Fadein;
                    final NiftyDialogBuilder niftyDialog = NiftyDialogBuilder.getInstance(MyInformationActivity.this);
                    niftyDialog
                            .withTitle("退出")
                            .withTitleColor("#ffffffff")
                            .withDialogColor("#EDEDED")
                            .isCancelableOnTouchOutside(true)
                            .withDuration(700)
                            .withEffect(effect)
                            .withButton1Text("放弃")
                            .withButton2Text("继续编辑")
                            .setCustomView(R.layout.return_content_arr, MyInformationActivity.this)
                            .setButton1Click(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    niftyDialog.dismiss();
                                    finish();
                                }
                            })
                            .setButton2Click(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    niftyDialog.dismiss();
                                }
                            })
                            .show();
                } else {
                    finish();
                }
            }
        });

    }

    private void initData() {
        //显示登录后个人信息的接口
        RequestParams params =
                new RequestParams("http://tr.zzapi.gson.cn/api/user/get");
        x.http().get(params, new CallBackAdapter<String>() {
            @Override
            public void onSuccess(String s) {
                JSONObject obj = JSON.parseObject(s);
                //Log.i("obj-------",obj.toString());
                Boolean success = obj.getBoolean("success");
                if (success) {
                    JSONObject data = obj.getJSONObject("data");
                    //设置默认的昵称
                    etReviseNickName.setText(data.getString("nickName"));
                    //设置性别
                    if (data.getString("gender").equals("BOY")) {
                        tvReviseSex.setText("男");
                    } else if (data.getString("gender").equals("GIRL")) {
                        tvReviseSex.setText("女");
                    } else if (data.getString("gender").equals("UNKONW")) {
                        tvReviseSex.setText("保密");
                    }
                    //设置邮箱
                    tvReviseEmail.setText(data.getString("email"));
                    //设置区域
                    if (data.getString("area").equals("LL")) {
                        tvReviseArea.setText("醴陵");
                    } else if (data.getString("area").equals("SF")) {
                        tvReviseArea.setText("石峰区");
                    } else if (data.getString("area").equals("YL")) {
                        tvReviseArea.setText("炎陵县");
                    } else if (data.getString("area").equals("YLSFFQ")) {
                        tvReviseArea.setText("云龙示范区");
                    } else if (data.getString("area").equals("ZZX")) {
                        tvReviseArea.setText("株洲县");
                    } else if (data.getString("area").equals("YX")) {
                        tvReviseArea.setText("攸县");
                    } else if (data.getString("area").equals("TY")) {
                        tvReviseArea.setText("天元区");
                    } else if (data.getString("area").equals("LS")) {
                        tvReviseArea.setText("芦淞区");
                    } else if (data.getString("area").equals("HT")) {
                        tvReviseArea.setText("荷塘区");
                    } else if (data.getString("area").equals("CL")) {
                        tvReviseArea.setText("茶陵县");
                    }
                }
            }
        });
    }


    private void reviseSex() {
        llChangeBgSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WheelViewDialog dialog = new WheelViewDialog(MyInformationActivity.this);
                dialog.setTitle("性 别")
                        .setItems(sex)
                        .setButtonText("确 定")
                        .setButtonSize(16)
                        .setCount(5)
                        .setDialogStyle(Color.parseColor("#ffff4444"))
                        .setOnDialogItemClickListener(new WheelViewDialog.OnDialogItemClickListener() {
                            @Override
                            public void onItemClick(int position, String s) {
                                //Log.i("position---",""+ position + s);
                                tvReviseSex.setText(s);
                                tvReviseSex.setTextColor(Color.parseColor("#ffff4444"));
                                isRevise = true;
                            }
                        })
                        .show();
            }
        });
    }

    private void reviseArea() {
        llChangeBgArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WheelViewDialog dialog = new WheelViewDialog(MyInformationActivity.this);
                dialog.setTitle("区域列表")
                        .setItems(area)
                        .setButtonText("确 定")
                        .setButtonSize(16)
                        .setCount(5)
                        .dismiss()
                        .setDialogStyle(Color.parseColor("#ffff4444"))
                        .setOnDialogItemClickListener(new WheelViewDialog.OnDialogItemClickListener() {
                            @Override
                            public void onItemClick(int position, String s) {
                                //Log.i("position---",""+ position + s);
                                tvReviseArea.setText(s);
                                tvReviseSex.setTextColor(Color.parseColor("#ffff4444"));
                                isRevise = true;
                            }
                        })
                        .show();
            }
        });
    }

}
